import 'package:flutter/material.dart';
import 'package:yp/helper.dart';
import 'package:yp/services/api.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';

import 'state.dart';

class IssueInvoiceLogic extends GetxController {
  final state = IssueInvoiceState();

  @override
  void onInit() {
    super.onInit();
    print('+++++++++++++++++   Get.arguments = ${Get.arguments}');
    state.paperInvoiceBool = true;
    state.invoiceHeaderBool = true;
    state.chooseIndex = -1;
    // state.personalInvoiceName = '';

    state.invoiceHeaderIndex = -1;
    state.invoiceNameController = TextEditingController(text: '');
    state.dutyParagraphController = TextEditingController(text: '');
    state.businessAddressController = TextEditingController(text: '');
    state.enterpriseTelephoneController = TextEditingController(text: '');
    state.bankOfDepositController = TextEditingController(text: '');
    state.bankAccountController = TextEditingController(text: '');

  }

  @override
  void onReady() {
    super.onReady();

    var params = {
      'usr_num_id': SpUtil.getInt('empeNumId'),
      'access_token': SpUtil.getString('token'),
    };
    //获取选择商品列表数据
    Api.addressList(params).listen((event) {
      print('地址获取成功 ===== event = $event');
      if (event['code'] == 0) {
        List list = event["member_adrs"];
        if (list != null && list.length > 0) {
          state.addressList = event["member_adrs"];
        } else {
          state.addressList = [];
        }
      } else {
        state.addressList = [];
      }
      update();
    }, onError: (error) {
      print('地址回去失败 ===== error = $error');
    });

    var params1 = {
      'usr_num_id': SpUtil.getInt('empeNumId'),
      'access_token': SpUtil.getString('token'),
    };
    Api.queryInvoiceHeader(params1).listen((event) {
      print('++++++++++++++********* 查询会员发票抬头  成功  event = $event');
      if (event['code'] == 0) {
        List list = event['invoice_title_infos'];
        if (list != null && list.length > 0) {
          state.queryInvoiceHeaderList = list;
        } else {
          state.queryInvoiceHeaderList = [];
        }
      } else {
        state.queryInvoiceHeaderList = [];
      }
      update();
    }, onError: (error) {
      print('++++++++++++++********* 查询会员发票抬头  失败  error = $error');
    });
    // var params = {
    //   'use_platform': 2,
    //   'access_token': SpUtil.getString('token'),
    //   'usr_num_id': SpUtil.getInt('empeNumId'),
    // };
    // Api.commonInvoiceHeader(params).listen((event) {
    //   print('+++++++++**************  成功  常用发票抬头  event = $event');
    //   if (event['code'] == 0) {
    //     List list = event['invoice_title_infos'];
    //     if (list.length > 0) {
    //       state.commonInvoiceHeader = list;
    //     }
    //   }
    //   update();
    // }, onError: (error) {
    //   print('+++++++++**************  失败  常用发票抬头  error = $error');
    // });
  }

  void ticketReceiptInformation(int index) {
    state.chooseIndex = index;
    // state.addressStr = '江苏省 苏州市 昆山市 花桥镇 绿地大道211号 启航城晶彩小区 36号402室$index';
    update();
  }
  void invoiceHeaderClick(int index, int indexType) {
    state.invoiceHeaderIndex = index;
    if (indexType == 1) {
      // state.personalInvoiceName = '广州腾讯科技有限公司';
    } else if (indexType == 2) {
      state.invoiceNameController.text = '广州腾讯科技有限公司';
    }
    update();
  }

  // 保存 开具发票 信息
  void preservationIssueInvoice() {
    if (state.invoiceHeaderBool) {
      if (state.invoiceHeaderMap == null) {
        toast('请选择发票名称');
        return ;
      }
    } else {
      if (state.invoiceNameController.text == '') {
        toast('请选择发票名称');
        return ;
      }
      if (state.dutyParagraphController.text == '') {
        toast('请输入税号');
        return ;
      }
      if (state.businessAddressController.text == '') {
        toast('请输入企业地址');
        return ;
      }
      if (state.enterpriseTelephoneController.text == '') {
        toast('请输入企业电话');
        return ;
      }
      if (state.bankOfDepositController.text == '') {
        toast('请输入开户银行');
        return ;
      }
      if (state.bankAccountController.text == '') {
        toast('请输入银行账号');
        return ;
      }
    }

    if (state.chooseIndex < 0) {
      toast('请选择收票人信息');
      return ;
    }
    Map<String, dynamic> map = state.addressList[state.chooseIndex];

    var params = {
      'tml_num_id': Get.arguments['tml_num_id'], // 订单号
      'invoice_model': {
        'invoice_medium': state.paperInvoiceBool ? '1' : '2',  // 发票介质: 1.纸质发票 2.电子发票
        'invoice_type': state.invoiceHeaderBool ? '1' : '2',  // 发票类型: 1.个人 2.公司
        'invoice_title': state.invoiceHeaderBool ? state.invoiceHeaderMap['invoice_title'] : state.invoiceNameController.text, // 发票抬头
        'consumer_invoice_content': '1', //'商品明细：发票内容将显示商品名称与价格信息，发票金额为实际支付金额，不含虚拟资产、优惠券等扣减金额。',  // 发票内容 1.商品明细
        'consumer_tax_account': state.dutyParagraphController.text,  // 购买方纳税人识别号
        'consumer_bank_name': state.bankOfDepositController.text, // 购买方开户银行
        'consumer_bank_account': state.bankAccountController.text,  // 购买方开户银行账号
        'consumer_telephone': state.enterpriseTelephoneController.text,  // 购买方电话
        'taker_telephone': map['contact_no'],  // 收票人电话
        'taker_prv_num_id': map['prv_num_id'],  // 收票人的省
        'taker_city_num_id': map['city_num_id'],  // 收票人的市
        'taker_city_area_num_id': map['city_area_num_id'], // 收票人的县，区
        'taker_addr': map['adr'], // 收票人地址
      },
    };
    print('params = $params');

    Api.createInvoice(params).listen((event) {
      print('保存 开具发票 成功 =============  event = $event');
      if (event['code'] == 0) {
        toast('保存成功');
        Get.back();
      } else {
        toast('保存失败');
      }
    }, onError: (error) {
      print('保存 开具发票 失败 =============  error = $error');
    });
  }
}